package com.longdaji.decoration.ui.activitiesOfMine.pendingPayment.payment;

import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.PayBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/3/12 0012.
 */

public class PaymentPresenter extends RxPresenter<PaymentContract.IView> implements PaymentContract.IPresenter {
    private DataManager mDataManager;

    @Inject
    public PaymentPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }

    @Override
    public void onAlipayClick(String money) {
        Map<String, Object> options = new HashMap<>();
        options.put("body", "桃心商品支付");
        options.put("subject", "桃心");
        options.put("out_trade_no", "199992");
        options.put("timeout_express", "15m");
        options.put("total_amount", "0.01");
//        options.put("total_amount", money);
        addSubscribe(mDataManager.getPayOrderInfo(options)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<PayBean>handleResult())
                .subscribeWith(new CommonSubscriber<PayBean>() {
                    @Override
                    public void onSuccess(PayBean payBean) {
                        LogUtil.d("succeed getPayOrderInfo : orderInfo =" + payBean.getOrderInfo());
                        if (mView != null) {
                            mView.showPayOrderInfoSucceed(payBean.getOrderInfo());
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed getPayOrderInfo : msg is " + msg);
                    }
                }));
    }

    @Override
    public void initData(String orderNo) {
        addSubscribe(mDataManager.doGetOrderApi(Account.getInstance().getUserid(), null, orderNo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<Orders>handleResult())
                .subscribeWith(new CommonSubscriber<Orders>() {
                    @Override
                    public void onSuccess(final Orders orders) {
                        LogUtil.d("succeed : doGetOrder ");
                        mView.showGetOrderSucceed(orders.getOrders().getOrder().get(0));
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);
                    }
                }));
    }
}
